package com.skateboard.reelviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.skateboard.reelviewtest.adapter.List3DRotateAdapter;
import com.skateboard.reelviewtest.path.BigCardLightBorder2View;
import com.skateboard.reelviewtest.path.BigCardLightBorderView;
import com.skateboard.reelviewtest.rotate.Rotate3dAnimation;
import com.skateboard.reelviewtest.utils.SimpleUtils;

public class List3RotateActivity extends AppCompatActivity implements List3DRotateAdapter.OnItemClickLitener {
    RecyclerView recyclerView;
    RelativeLayout rootView;
    RelativeLayout layout_imageview;
    private Rotate3dAnimation rotateAnimation1;
    private ScaleAnimation scaleAnimation;

    RelativeLayout childView ;

    LottieAnimationView animation_view;

    BigCardLightBorder2View liuguang_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3_rotate);

        animation_view=findViewById(R.id.animation_view);
        animation_view.setAnimation("hello-world.json");
        animation_view.setImageAssetsFolder("images");


        recyclerView=findViewById(R.id.recyclerView);
        rootView=findViewById(R.id.rootView);
        layout_imageview=findViewById(R.id.layout_imageview);
        List3DRotateAdapter adapter3D=new List3DRotateAdapter();
        recyclerView.setAdapter(adapter3D);

        recyclerView.setLayoutManager(new GridLayoutManager(this,2));

        adapter3D.setmOnItemClickLitener(this);

        liuguang_view=findViewById(R.id.liuguang_view);
    }

    @Override
    public void onItemClick(View view, int position) {
        doAnimation(view);
    }


    public void doAnimation(View view){
        RelativeLayout relativeLayout= (RelativeLayout) view;
        for(int i=0;i<relativeLayout.getChildCount();i++){
            View child=relativeLayout.getChildAt(i);
            if(child.getId()==R.id.item_1){
                childView= (RelativeLayout) child;
            }
        }
        if(childView==null){
            return;
        }

        final int[] location=new int[2];
        childView.getLocationOnScreen(location);
        final int[]recycleLocation=new int[2];
        recyclerView.getLocationOnScreen(recycleLocation);

        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) layout_imageview.getLayoutParams();
        layoutParams.leftMargin=location[0];
        layoutParams.topMargin=location[1]-recycleLocation[1];
        layout_imageview.setLayoutParams(layoutParams);


        int i=rootView.getHeight()/2;
        int j= (int) (SimpleUtils.dp2px(this,220)/2);
        float k=location[1]-recycleLocation[1];
        int yTrans= (int) (i-j-k);
        Log.e("ssss",yTrans+"====result====");
        Log.e("ssss","top:"+childView.getTop()+"========left:"+childView.getLeft()+"======x:"+childView.getX()+"y:"+childView.getY());
        int xTrans= (int) (rootView.getWidth()/2-SimpleUtils.dp2px(this,150)/2-location[0]);

        TranslateAnimation translateAnimation = new TranslateAnimation(
                //X轴初始位置
                0,
                //X轴移动的结束位置
                xTrans,
                //y轴开始位置
                0,
                //y轴移动后的结束位置
                yTrans);
        translateAnimation.setDuration(1000);
        //如果fillAfter的值为真的话，动画结束后，控件停留在执行后的状态
        translateAnimation.setFillAfter(true);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) layout_imageview.getLayoutParams();
                params.topMargin=rootView.getHeight()/2-layout_imageview.getHeight()/2;
                params.leftMargin=rootView.getWidth()/2-layout_imageview.getWidth()/2;
                layout_imageview.setLayoutParams(params);
                AnimationSet animationSet=new AnimationSet(true);
                animationSet.addAnimation(rotateAnimation1);
                animationSet.addAnimation(scaleAnimation);
                animationSet.setDuration(3000);
                animationSet.setFillAfter(true);
                layout_imageview.startAnimation(animationSet);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //启动动画
        layout_imageview.startAnimation(translateAnimation);
        layout_imageview.setVisibility(View.VISIBLE);
        childView.setVisibility(View.GONE);

        float centerX=SimpleUtils.dp2px(this,150)/2;
        float centerY=SimpleUtils.dp2px(this,220)/2;
        float depthZ=1.0f;
        rotateAnimation1 = new Rotate3dAnimation(this,0, 360, centerX, centerY, depthZ, true);

        scaleAnimation = new ScaleAnimation(1F,2.193f,
                1F,2.381f,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation_view.playAnimation();
                liuguang_view.startAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }
}